//历史数据操作工具类

export default {
	install(Vue, options) {
		var vm = new Vue(options)
		// 在Vue的原型上添加实例方法，以全局调用
		Vue.prototype.$historyTools = {
			//判断值是否有效
			isNull(value) {
				try{
					if (value == undefined || isNaN(parseFloat(value))) {
						return true
					}
					if (value < -1.1 && value > -1.2) {
						return true
					}
					return false
				}catch(e){
					return true
				}
			},
			isEmpty(value) {
				return value == undefined || value.length == 0 || JSON.stringify(value) == "{}"
			},
			cutDigital(digital, len) {
				if (len == undefined) {
					len = 2;
				}
				if (len < 0) {
					return 0;
				}
				var res = (Math.round(digital * Math.pow(10, len))) / Math.pow(10, len)
				if (isNaN(res)) {
					return
				}
				res += ''
				//补全0到位数达标
				var now = 0
				if (res.indexOf('.') != -1) {
					now = res.substr(res.indexOf('.') + 1).length
				} else {
					res += '.'
				}
				for (var i = now; i < len; i++) {
					res += '0'
				}
				return res;
			},
			//如果第二个参数有效，就用第二个，无效就用第一个参数数组求平均值
			getAvg(list, avg) {
				if (!this.isNull(avg)) {
					return avg
				}
				if (this.isEmpty(list)) {
					return
				}
				var count = 0
				var sum = 0
				for (var i = 0, len = list.length; i < len; i++) {
					var value = list[i]
					if (!this.isNull(value)) {
						sum += value
						count++
					}
				}
				if (count == 0) {
					return
				}
				return this.cutDigital(sum / count)
			},
			//获取一天数据中的差值，用于累加值
			getSub(list) {
				if (this.isEmpty(list)) {
					return
				}
				var bg
				var end
				for (var i = 0, len = list.length; i < len; i++) {
					var value = list[i]
					if (!this.isNull(value)) {
						end = value
						if (bg == undefined) {
							bg = value
						}
					}
				}
				if (bg == undefined) {
					return
				}
				return this.cutDigital(end - bg)
			},
			getMax(list, maxValue) {
				var max
				for (var i = 0, len = list.length; i < len; i++) {
					var item = list[i]
					if (!this.isNull(item)) {
						if (max == undefined || max < item) {
							max = item
						}
					}
				}
				return this.isNull(maxValue) ? max : (max > maxValue ? max : maxValue)
			},
			getMin(list, minValue) {
				var min
				for (var i = 0, len = list.length; i < len; i++) {
					var item = list[i]
					if (!this.isNull(item)) {
						if (min == undefined || min > item) {
							min = item
						}
					}
				}
				return this.isNull(minValue) ? min : (min < minValue ? min : minValue)
			},
			getTimeTextList() {
				var list = []
				for (var i = 0; i < 24; i++) {
					for (var m = 0; m < 60; m += 5) {
						list.push((i >= 10 ? "" : "0") + i + ":" + (m >= 10 ? "" : "0") + m)
					}
				}
				return list
			},
			getTimeKeyList() {
				var list = []
				for (var i = 0; i < 24; i++) {
					for (var m = 0; m < 60; m += 5) {
						list.push("value"+(i >= 10 ? "" : "0") + i + (m >= 10 ? "" : "0") + m)
					}
				}
				return list
			},
		}
	}
}
